//
// DatagramSocket.cpp
//
// $Id: //poco/1.4/Net/src/DatagramSocket.cpp#1 $
//
// Library: Net
// Package: Sockets
// Module:  DatagramSocket
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//



#include "Poco/Exception.h"
#include "Poco/Net/DatagramSocket.h"

using Poco::InvalidArgumentException;


namespace Poco {
namespace Net {


DatagramSocket::DatagramSocket():Socket(SOCK_DGRAM)
{
	init(AF_INET);
}

DatagramSocket::DatagramSocket(IPAddress::Family family)
{
	setSocketType(SOCK_DGRAM);
	if (family == IPAddress::IPv4)
		init(AF_INET);
#if defined(POCO_HAVE_IPv6)
	else if (family == IPAddress::IPv6)
		init(AF_INET6);
#endif
	else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl");
}


DatagramSocket::DatagramSocket(const SocketAddress& address, bool reuseAddress)
{
	setSocketType(SOCK_DGRAM);
	Socket::bind(address, reuseAddress);
}


DatagramSocket::DatagramSocket(const Socket& socket): Socket(socket)
{
	if (socket.getSocketType() != SOCK_DGRAM)
		throw InvalidArgumentException("Cannot assign incompatible socket");
}

DatagramSocket::DatagramSocket(poco_socket_t sockfd):Socket(sockfd,SOCK_DGRAM)
{
	
}

DatagramSocket::~DatagramSocket()
{
}


DatagramSocket& DatagramSocket::operator = (const Socket& socket)
{
	if (socket.getSocketType() != SOCK_DGRAM)
		throw InvalidArgumentException("Cannot assign incompatible socket");
	Socket::operator =(socket);
	return *this;
}

void DatagramSocket::connect(const SocketAddress& address)
{
	Socket::connect(address);
}


void DatagramSocket::bind(const SocketAddress& address, bool reuseAddress)
{
	Socket::bind(address, reuseAddress);
}

int DatagramSocket::sendTo(const char* buffer, int length, const SocketAddress& address, int flags)
{
	return Socket::sendTo(buffer, length, address, flags);
}


int DatagramSocket::receiveFrom(char* buffer, int length, SocketAddress& address, int flags)
{
	return Socket::recvFrom(buffer, length, address, flags);
}

} } // namespace Poco::Net
